  
wx-App({
  /**
   * 当小程序初始化完成时，会触发 onLaunch（全局只触发一次）
   */
 
  onLaunch: function () {
      wx.login({
        success(res){
          console.log(res,45)
        }
      })
      // console.log(wx.getStorageSync("token"));
      wx.newWx = wxApiInterceptors({
        request: {
          async request(params) {
              if (params.header === undefined) {
                  params.header = {};
              }
              let token = wx.getStorageSync('token');
              if (!token) {
                  ({data: token} = await wx.request('http://api_dev.wanxikeji.cn/api/refreshToken'));
                  wx.setStorageSync('token', token);
              }
              params.header.token = token;
              return params;
          },
        },
      }, true);
      wx.request({
        url: 'http://api_dev.wanxikeji.cn/api/shoppingCarAddModify',
        method:'POST',
        data: {
          token:wx.getStorageSync("token"),
          good_id:1,
          num:2,
          price:"228.00",
          money:"456.00",
          sku:{
            capacity:"500m/瓶"
          },
          shopping_car_id:"101"
        },
        success: (res) => {
        }
      })
      wx.request({
        url: 'http://api_dev.wanxikeji.cn/api/shoppingCarList',
        method:'POST',
        data: {
          token:wx.getStorageSync("token"),
        },
        success: (res) => {
        }
      })
      wx.request({
        url: 'http://api_dev.wanxikeji.cn/api/goodList',
        method:'POST',
        data: {
          token:wx.getStorageSync("token"),
        },
        success: (res) => {
          // console.log(res,5555555555555);
        }
      })
      // wx.request({//模拟添加购物车
      //   url: 'http://api_dev.wanxikeji.cn/api/shoppingCarAddModify',
      //   method:'POST',
      //   data: {
      //     token:wx.getStorageSync("token"),
      //     good_id:926,
      //     num:1,
      //     price:799.00,
      //     money:799.00,
      //     sku:'{"sku":"馥郁香型","pic":"750ml","price":"799.00"}'
      //   },
      //   success: (res) => {
      //     console.log(res,6666666666666);
      //   }
      // })
      // wx.request({//模拟购物车列表
      //   url: 'http://api_dev.wanxikeji.cn/api/shoppingCarList',
      //   method:'POST',
      //   data: {
      //     token:wx.getStorageSync("token"),
      //   },
      //   success: (res) => {
      //     console.log(res,7777777777);
      //   }
      // })
      // wx.request({//删除购物车列表
      //   url: 'http://api_dev.wanxikeji.cn/api/shoppingCarDelete',
      //   method:'POST',
      //   data: {
      //     token:wx.getStorageSync("token"),
      //     shopping_car_id:32
      //   },
      //   success: (res) => {
      //     console.log(res,7777777777);
      //   }
      // })
  },

  /**
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  // onShow: function (options) {
  //   var that = this;
  //   // 初始化购物车
  //   that.scanCart(that)
  // },
  onShow: function() {
    var that = this;
    that.scanCart(that)
  },
  /**
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide: function () {
    
  },
  scanCart: function(that){
    let cart = 0;
    if (wx.getStorageSync('cartLen')){
      cart = wx.getStorageSync('cartLen');    
    }
    if (cart!=0) {
      wx.setTabBarBadge({
        index: 2,
        text: String(cart),
        fail: function(){
          return
        }
      })
    }else {
      wx.hideTabBarRedDot({
        index: 2,
        fail: function(){
          return
        }
      })
    }
  },
  onLaunch(options) {
    var that = this;
    that.scanCart(that)
  },
  /**
   * 当小程序发生脚本错误，或者 api 调用失败时，会触发 onError 并带上错误信息
   */
  onError: function (msg) {
  },
  //options(path,query,isEntryPage)
  onPageNotFound:function(options){

  }
})
